package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.BalanceBillindexDO;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-10-18
 */
public interface BalanceBillindexMapper extends BaseMapper<BalanceBillindexDO> {

    String QUERY_SQL = "SELECT a.Index_ID, a.Index_GUID, case a.Index_CustomerID when 0 then null else a.Index_CustomerID end as indexCustomerid, a.Index_SupplierID, a.Index_Code, a.Index_No, a.Index_Amount, a.Index_Description, a.Index_Status, a.Index_StatusTime, a.Index_Author, a.Index_InsertTime, a.Index_Updater, a.Index_UpdateTime, a.Index_Invalid, a.Index_Comments, a.Index_BillType, a.Index_Bcode, a.Index_OwnerCompanyID, a.Index_SrcID, a.Index_RefReason, a.Index_SupplierSymbolid, a.Index_DriverID, a.Index_BalanceTag, a.Index_Bname, a.Index_BillingStatus, a.Index_BillingDate, a.Index_BillingDesc, a.Index_AsyncFlag " +
            ",CASE when d.Customer_OwnerCompany=d.Customer_CompanyID THEN 2 ELSE d.Customer_UnderLine end as customerType,d.Customer_Name as customerName,d.Customer_OwnerCompany as downTenantId " +
            "FROM Balance_BillIndex a " +
            "LEFT JOIN TMS_MCustomer d on a.Index_CustomerID = d.Customer_ID " +
            "WHERE a.Index_OwnerCompanyID > 0 ";

    String QUERY_SQL_TOP = "SELECT top 1 a.Index_ID, a.Index_GUID, case a.Index_CustomerID when 0 then null else a.Index_CustomerID end as indexCustomerid, a.Index_SupplierID, a.Index_Code, a.Index_No, a.Index_Amount, a.Index_Description, a.Index_Status, a.Index_StatusTime, a.Index_Author, a.Index_InsertTime, a.Index_Updater, a.Index_UpdateTime, a.Index_Invalid, a.Index_Comments, a.Index_BillType, a.Index_Bcode, a.Index_OwnerCompanyID, a.Index_SrcID, a.Index_RefReason, a.Index_SupplierSymbolid, a.Index_DriverID, a.Index_BalanceTag, a.Index_Bname, a.Index_BillingStatus, a.Index_BillingDate, a.Index_BillingDesc, a.Index_AsyncFlag " +
            ",CASE when d.Customer_OwnerCompany=d.Customer_CompanyID THEN 2 ELSE d.Customer_UnderLine end as customerType,d.Customer_Name as customerName,d.Customer_OwnerCompany as downTenantId " +
            "FROM Balance_BillIndex a " +
            "LEFT JOIN TMS_MCustomer d on a.Index_CustomerID = d.Customer_ID " +
            "WHERE a.Index_OwnerCompanyID > 0 ";

    /**
     * 对账单
     *
     * @return
     */
    @Select(QUERY_SQL)
    @DataScope(column = "Index_OwnerCompanyID", alias = "a")
    List<BalanceBillindexDO> list();

    /**
     * 根据id查询
     *
     * @param indexId
     * @return
     */
    @Select(QUERY_SQL + "and a.Index_ID = #{indexId}")
    BalanceBillindexDO getById(Long indexId);

    /**
     * 根据srcId查询
     *
     * @param indexId
     * @return
     */
    @Select(QUERY_SQL_TOP + "and a.Index_SrcID = #{indexId} and (a.Index_Status = 2 or a.Index_Status = 4 or a.Index_Status = 8) order by a.Index_InsertTime desc")
    BalanceBillindexDO getBySrcId(Long indexId);
}
